package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.AESUtil;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class MineMsgPassword extends BaseActivity implements View.OnClickListener{

    private ImageView passwordback;
    private Button passcode;
    private EditText passincode;
    private EditText passphone;
    private EditText password;
    private Button upword;
    private String code;
    private String number;
    private String word;
    private String aWord;
    private EditText passwordagain;
    private String wordagain;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String data;
    private String messageId;
    private String messageId1;
    private SharedPreferences.Editor edit;
    private SharedPreferences ss;
    private String messageId2;
    private SharedPreferences s;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_msg_password);
        initpasswordView();

        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        ss = getSharedPreferences("messageId", Context.MODE_PRIVATE);
        messageId1 = ss.getString("messageId2", "3");
    }
    private void initpasswordView() {
        passwordback = (ImageView) findViewById(R.id.msg_password_back);
        passcode = (Button) findViewById(R.id.password_code);
        passincode = (EditText) findViewById(R.id.password_inputcode);
        password = (EditText) findViewById(R.id.password_word);
        passwordagain = (EditText) findViewById(R.id.password_wordagarn);
        upword = (Button) findViewById(R.id.msg_uppasswordbt);
        backmain = (ImageView) findViewById(R.id.minemsgpsg_backmain);
        // 设置监听
        backmain.setOnClickListener(this);
        passwordback.setOnClickListener(this);
        passcode.setOnClickListener(this);
        upword.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.minemsgpsg_backmain:
                startActivity(new Intent(MineMsgPassword.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.msg_password_back:
                finish();
                break;
            case R.id.password_code:// 获取验证码
                getInText();
                if (word.equals(wordagain)){
                    MyTimer timer = new MyTimer(120000,1000);
                    timer.start();
                    getCode();
                }else{
                    Toast.makeText(this, "请确认两次输入密码是否一致", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.msg_uppasswordbt:// 修改密码
                getWord();
                break;
        }
    }
        //修改密码
    private void getWord() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/user/modifyPassword");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        getInText();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("password",aWord);
        jsonObject.put("messageId",messageId1);
        jsonObject.put("code",code);
        params.setBodyContent(jsonObject.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                finish();
                Toast.makeText(MineMsgPassword.this, "设置成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(MineMsgPassword.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    // 获取验证码
    public void getCode() {

        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/verify/sendCode");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        getInText();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type","2");
        params.addParameter("params",jsonObject.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                // 获取messageid
                data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                messageId2 = obj1.getString("messageId");

                s = getSharedPreferences("messageId", Context.MODE_PRIVATE);
                edit = s.edit();
                edit.putString("messageId2", messageId2);
                edit.commit();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MineMsgPassword.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                ErrType.errType(MineMsgPassword.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 获取输入内容
    private void getInText() {
        code = String.valueOf(passincode.getText());
        wordagain = String.valueOf(passwordagain.getText());
        word = String.valueOf(password.getText());
        try {
            aWord = AESUtil.encryptAES(word);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class MyTimer extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                  倒计时数        to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *           倒计时间隔               {@link #onTick(long)} callbacks.
         */
        public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            passcode.setClickable(false);
            passcode.setText(millisUntilFinished/1000+" s 后发送");
        }

        @Override
        public void onFinish() {
            passcode.setClickable(true);
            passcode.setText("验证码");
        }
    }
}
